package com.lfs.dingtalk.robot.servcie;

import com.dingtalk.api.request.OapiRobotSendRequest;
import com.lfs.dingtalk.robot.domain.GroupChatSendRequest;

import java.util.Optional;

/**
 * 群消息发送策略
 */
public interface MessageStrategy {
    void prepareMessage(OapiRobotSendRequest req, GroupChatSendRequest.Type messageType);



    class TextMessageStrategy implements MessageStrategy {
        @Override
        public void prepareMessage(OapiRobotSendRequest req, GroupChatSendRequest.Type messageType) {
            GroupChatSendRequest.Text textType = (GroupChatSendRequest.Text) messageType;
            req.setText(new OapiRobotSendRequest.Text() {{
                setContent(textType.getText());
            }});
            this.setMessage(req, textType);
        }
    }

    class MarkdownMessageStrategy implements MessageStrategy {
        @Override
        public void prepareMessage(OapiRobotSendRequest req, GroupChatSendRequest.Type messageType) {
            GroupChatSendRequest.MarkDown markDownType = (GroupChatSendRequest.MarkDown) messageType;
            req.setMarkdown(new OapiRobotSendRequest.Markdown() {{
                setTitle(markDownType.getTitle());
                setText(markDownType.getText());
            }});
            this.setMessage(req, markDownType);
        }
    }




    /**
     * 群消息公共设置
     * @param req 请求体
     * @param messageType 请求对象
     */
    default void setMessage(OapiRobotSendRequest req, GroupChatSendRequest.Type messageType) {
        req.setMsgtype(messageType.getMsgtype());
        Optional.ofNullable(messageType.getAt()).ifPresent(at -> req.setAt(new OapiRobotSendRequest.At() {{
            setIsAtAll(at.getIsAtAll());
            setAtMobiles(at.getAtMobiles());
            setAtUserIds(at.getAtUserIds());
        }}));
    }
}
